//
//  ListDatabase.m
//  ABCDEFG
//
//  Created by Xue Senhong on 15/5/21.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "ListDatabase.h"

@implementation ListDatabase
+ (void)commitSql:(NSString *)str
{
    sqlite3 *db = [DB openDB];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"成功");
    } else {
        NSLog(@"失败");
    }
    
}

+ (NSMutableArray *)selectAll
{
    NSString *str = @"select ZBRAND,ZNAME,ZPRICERANGE,ZIMAGEURL,ZPARENTNAME from ZSERIES";
    sqlite3 *db = [DB openDB];
    // 定义执行结果接受者
    sqlite3_stmt *stmt = nil;
    
    // &stmt查询的结果所在的位置, -1代表不限制长度
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            NSInteger zpk = sqlite3_column_int(stmt, 0);
            const unsigned char *zFirst = sqlite3_column_text(stmt, 1);
            const unsigned char *zImage = sqlite3_column_text(stmt, 2);
            const unsigned char *zName = sqlite3_column_text(stmt, 3);
            const unsigned char *zId = sqlite3_column_text(stmt, 4);
            
            
            NSString *first = [NSString stringWithUTF8String:(const char *)zFirst];
            NSMutableString *image = [NSMutableString stringWithUTF8String:(const char *)zImage];
            NSString *name = [NSString stringWithUTF8String:(const char *)zName];
            NSString *iD = [NSString stringWithUTF8String:(const char *)zId];
            
            ListModel *model = [[ListModel alloc] init];
            model.ZNAME = first;
            model.ZPRICERANGE = image;
            model.ZIMAGEURL = name;
            model.ZPARENTNAME = iD;
            model.ZBRAND = zpk;
            
            
            
            [array addObject:model];
        }
    }
    return array;
}

+ (NSMutableArray *)selectWithBrand:(NSInteger)ZBrand
{
    NSString *str = [NSString stringWithFormat:@"select ZBRAND,ZNAME,ZPRICERANGE,ZIMAGEURL,ZPARENTNAME,ZID from ZSERIES where ZBRAND=%ld", ZBrand];
    sqlite3 *db = [DB openDB];
    // 定义执行结果接受者
    sqlite3_stmt *stmt = nil;
    
    // &stmt查询的结果所在的位置, -1代表不限制长度
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            NSInteger zpk = sqlite3_column_int(stmt, 0);
            const unsigned char *zFirst = sqlite3_column_text(stmt, 1);
            const unsigned char *zImage = sqlite3_column_text(stmt, 2);
            const unsigned char *zName = sqlite3_column_text(stmt, 3);
            const unsigned char *zId = sqlite3_column_text(stmt, 4);
            const unsigned char *mId = sqlite3_column_text(stmt, 5);

            if (zFirst != nil && zImage != nil && zName != nil && zId != nil) {
                NSString *first = [NSString stringWithUTF8String:(const char *)zFirst];
                NSMutableString *image = [NSMutableString stringWithUTF8String:(const char *)zImage];
                NSString *name = [NSString stringWithUTF8String:(const char *)zName];
                NSString *iD = [NSString stringWithUTF8String:(const char *)zId];
                NSString *myId = [NSString stringWithUTF8String:(const char *)mId];

                ListModel *model = [[ListModel alloc] init];
                model.ZNAME = first;
                model.ZPRICERANGE = image;
                model.ZIMAGEURL = name;
                model.ZPARENTNAME = iD;
                model.ZBRAND = zpk;
                model.ZID = myId;
                
                [array addObject:model];

            }
         }
    }
    return array;
}

@end
